package com.zzh.takeoutapp.model;

import android.content.Context;
import android.util.Log;

import com.zzh.takeoutapp.config.UrlConfig;
import com.zzh.takeoutapp.entity.FoodBean;
import com.zzh.takeoutapp.entity.HomeShopEntity;
import com.zzh.takeoutapp.entity.OrderListBean;
import com.zzh.takeoutapp.entity.TypeBean;
import com.zzh.takeoutapp.utils.Encrypt;
import com.zzh.takeoutapp.utils.GsonResolve;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * Created by sunfusheng on 16/4/22.
 */
public class ModelUtil {

    public static ModelUtil instence;

    public static List<String> getBannerData() {
        List<String> adList = new ArrayList<>();
        adList.add("http://img2.imgtn.bdimg.com/it/u=2850936076,2080165544&fm=206&gp=0.jpg");
        adList.add("http://img3.imgtn.bdimg.com/it/u=524208507,12616758&fm=206&gp=0.jpg");
        adList.add("http://img3.imgtn.bdimg.com/it/u=698582197,4250615262&fm=206&gp=0.jpg");
        return adList;
    }

    /*Set set = new HashSet();
           List newList = new ArrayList();
           for (Iterator iter = deviceEntities.iterator(); iter.hasNext(); ) {
               Object element = iter.next();
               if (set.add(element))
                   newList.add(element);
           }
           deviceEntities.clear();
           deviceEntities.addAll(newList);*/
    public static HomeBean getHomeData(JSONObject jsonObject) {
        HomeBean beanList = GsonResolve.jsonString2Bean(jsonObject.toString(), HomeBean.class);
        return beanList;
    }

    public static HomeShopEntity getHomeShopEntity(JSONObject jsonObject) {
        HomeShopEntity beanList = GsonResolve.jsonString2Bean(jsonObject.toString(), HomeShopEntity.class);
        return beanList;
    }

    /**
     * 获取商家数据分类
     *
     * @return
     */
    public static List<TypeBean> getTypeBean(JSONObject jsonObject) {
        ArrayList<TypeBean> tList = new ArrayList<>();
        JSONObject data = jsonObject.optJSONObject("data");
        JSONObject detail = data.optJSONObject("detail");
        JSONArray items = detail.optJSONArray("items");
        for (int i = 0; i < items.length(); i++) {
            try {
                JSONObject title = (JSONObject) items.get(i);
                TypeBean bean = GsonResolve.jsonString2Bean(title.toString(), TypeBean.class);
                bean.setName(title.optString("title"));
                tList.add(bean);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return tList;
    }

    /**
     * 获取订单列表数据
     *
     * @param jsonArray
     * @return
     */
    public static List<OrderListBean> orderList(JSONArray jsonArray) {
        List<OrderListBean> orderListBeanList = new ArrayList<>();
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject object1 = jsonArray.optJSONObject(i);
            OrderListBean bean = GsonResolve.jsonString2Bean(object1.toString(), OrderListBean.class);
            bean.setDateline(bean.getDateline() + System.currentTimeMillis());
            orderListBeanList.add(bean);
        }
        return orderListBeanList;
    }

    public static List<FoodBean> getFoodBeanDatas(JSONObject jsonObject) {
        ArrayList<FoodBean> fList = new ArrayList<>();
        try {
            JSONObject data = jsonObject.optJSONObject("data");
            JSONObject detail = data.optJSONObject("detail");
            JSONArray items = detail.optJSONArray("items");
            for (int i = 0; i < items.length(); i++) {
                JSONObject title = (JSONObject) items.get(i);
                JSONArray products = title.optJSONArray("products");
                for (int j = 0; j < products.length(); j++) {
                    FoodBean foodBean = new FoodBean();
                    JSONObject data_shop = products.optJSONObject(j);
                    foodBean.setType(title.optString("title"));
                    foodBean.setIs_spec(data_shop.optString("is_spec"));
                    foodBean.setIs_discount(data_shop.optString("is_discount"));
                    foodBean.setProduct_id(data_shop.optString("product_id"));
                    foodBean.setShop_id(data_shop.optString("shop_id"));
                    foodBean.setId(data_shop.optString("cate_id") + j);
                    foodBean.setTitle(data_shop.optString("title"));
                    foodBean.setName(data_shop.optString("title"));
                    foodBean.setSale("月售" + data_shop.optString("sales"));
                    foodBean.setIntro(data_shop.optString("intro"));
                    foodBean.setPhoto(data_shop.optString("photo"));
                    JSONArray specification = data_shop.optJSONArray("specification");
                    ArrayList<FoodBean.SpecificationBean> specificationBeans = new ArrayList<>();
                    for (int k = 0; k < specification.length(); k++) {
                        JSONObject jsonObject1 = specification.optJSONObject(k);
                        FoodBean.SpecificationBean specificationBean = GsonResolve.jsonString2Bean(jsonObject1.toString(),
                                FoodBean.SpecificationBean.class);
                        specificationBeans.add(specificationBean);
                    }
                    JSONArray specs = data_shop.optJSONArray("specs");
                    ArrayList<FoodBean.SpecsBean> specsBeans = new ArrayList<>();
                    for (int h = 0; h < specs.length(); h++) {
                        JSONObject jsonObject1 = specs.getJSONObject(h);
                        FoodBean.SpecsBean specificationBean = GsonResolve.jsonString2Bean(jsonObject1.toString(),
                                FoodBean.SpecsBean.class);
                        specsBeans.add(specificationBean);
                    }
                    foodBean.setSpecsX(specsBeans);
                    foodBean.setSpecification(specificationBeans);
                    foodBean.setPrice(BigDecimal.valueOf(Double.parseDouble(data_shop.optString("price")))
                            .setScale(1, BigDecimal.ROUND_HALF_DOWN));
                    fList.add(foodBean);
                    Log.e("specification", specification.length() + "---" + specificationBeans.size());
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return fList;
    }


    public static ModelUtil getInstence() {
        if (instence == null) {
            instence = new ModelUtil();
        }
        return instence;
    }


}
